package com.c2b.algorithm.leetcode.jzoffer;

/**
 * <a href="https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef?tpId=13&tqId=23450&ru=/ta/sql-quick-study&qru=/ta/sql-quick-study/question-ranking">删除链表中重复的结点</a>
 * <p>在一个排序的链表中，存在重复的结点，请删除该链表中重复的结点，重复的结点不保留，返回链表头指针。 例如，链表 1->2->3->3->4->4->5  处理后为 1->2->5</p>
 *
 * @author c2b
 * @since 2023/3/7 16:46
 */
public class JzOffer0076DeleteDuplication_ {

    public ListNode deleteDuplication(ListNode pHead) {
        if (pHead == null || pHead.next == null) {
            return pHead;
        }
        //在链表前加一个表头
        ListNode res = new ListNode(0);
        res.next = pHead;
        ListNode currNode = res;
        while (currNode.next != null && currNode.next.next != null) {
            //遇到相邻两个节点值相同
            if (currNode.next.val == currNode.next.next.val) {
                int temp = currNode.next.val;
                //将所有相同的都跳过
                while (currNode.next != null && currNode.next.val == temp) {
                    currNode.next = currNode.next.next;
                }
            } else {
                currNode = currNode.next;
            }
        }
        //返回时去掉表头
        return res.next;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(3);
        head.next.next.next.next = new ListNode(4);
        head.next.next.next.next.next = new ListNode(4);
        head.next.next.next.next.next.next = new ListNode(5);
        JzOffer0076DeleteDuplication_ jzOffer0076DeleteDuplication = new JzOffer0076DeleteDuplication_();
        ListNode listNode = jzOffer0076DeleteDuplication.deleteDuplication(head);
        System.out.println();
    }
}
